﻿ var gc,lgi,agb,agf ,agfc,msg ,fl,delf,cdl,dlf,fi;
 var currrentGroupName,currentFriendID;
 var currentGroupID = 0;
 var egc,egf,gi,ug,eff,efc,ufg;
 var tip,gs,cuf;
 var kw,ag;
 $(document).ready(function(){
    gc = $('#groupContainer');
    lgi = $('#loadGroupImg');
    agb = $('#addGroupBtn');
    agf = $('#addGroupFrm');
    agfc= $('#agfContainer');
    lfi = $('#loadFriendImg');
    delf=$('#delFriend');
    dlf = $('#dfFrm');
    msg = $('#msg');
    cdl  = $('#cacelDel');
    fl = $('#friendList');
    fi = $('#friendID');
    egc = $('#editGroupContainer');
    egf = $('#editGroupFrm');
    gi = $('#groupID');
    ug = $('#editGroupName');
    eff = $('#editFriendFrm');
    efc = $('#eidtFriendContainer');
    ufg = $('#UpdateFriendGroup');
    gs = $('#groupSelector');
    tip = $('#tip');
    cuf = $('#cacelUpdateFriend');
    ufg = $('#UpdateFriendGroup');
    kw = $('#kw');
    ag = $('#allGroup');
    agb.bind('click',function(){agfc.fadeIn('slow');agfc.dialog({width:380,height:100,overlay: { opacity: 0.5, background: "black" },modal:true});});
    cdl.bind('click',function(){delf.dialog('close');return false;});
    cuf.bind('click',function(){efc.dialog('close');return false;});
    ufg.bind('click',doUpdateFriendGroup);
    ag.bind('click',function(){kw.val('');getAllFriend();});
    $('#findBtn').bind('click',find);
    getGroups();
    getAllFriend();
    agf.submit(function(){
         var postData = agf.serialize();            
         var action = agf.attr('action');
         $.ajax({
             type:'POST',
             url:action,
             data:postData,
             success:function(res){
                 getGroups();
                 agfc.hide('fast');
                 agfc.dialog('close');
             },
             error:function(){
                 alert('add group error!');                  
             }
         });
         return false;
     });
   dlf.submit(function(){
        var postData = dlf.serialize();           
        var action = dlf.attr('action');
        $.ajax({
             type:'POST',
             url:action,
             data:postData,
             success:function(res){
                 getFriend(currentGroupID,currrentGroupName);                    
                 delf.dialog('close');
             },
             error:function(){
                 alert('delete friend error!');                  
             }
         });
         return false;
   });
   egf.submit(function(){
        var postData = egf.serialize();           
        var action = egf.attr('action');
        $.ajax({
             type:'POST',
             url:action,
             data:postData,
             success:function(res){
                 getGroups();                
                 egc.dialog('close');
             },
             error:function(){
                 alert('update group error!');                  
             }
         });
          return false;
   });
 });
 
 function doUpdateFriendGroup(){
        var newGroupID = $("#groupSelector  input[@checked]").val();      
        var postData = "friendID="+currentFriendID+"&newGroupID="+newGroupID;                 
        var action = eff.attr('action');
        $.ajax({
             type:'POST',
             url:action,
             data:postData,
             success:function(res){
                 getFriend(currentGroupID,currrentGroupName);    
                 efc.dialog('close');
             },
             error:function(){
                 alert('update friend group setting  error!');                  
             }
         });
         return false;
 }
 
 function showIcon(op,i){     
      var icon = $("#"+op+i);
      icon.show();
 }
 
 function hideIcon(op,i){     
      var icon = $("#"+op+i);
      icon.hide();
 }
 
 function getGroups(){         
     $.ajax({
         type:'GET',
         url:'GetGroups',
         dataType:'json',
         success:function(res){                           
             var data = eval(res);
             gc.html('');
             var column = 3;
             var html="<tr>";
             $.each(data,function(i){
                 var href="javascript:getFriend("+data[i].ID+",'"+data[i].GroupName+"')";
                 gc.append("<li class='groupItem' onmouseout=javascript:hideIcon('tool',"+i+") onmouseover=javascript:showIcon('tool',"+i+");><a href="+href+">"+data[i].GroupName+"</a>&nbsp;&nbsp;&nbsp;&nbsp;<span id='tool"+i+"' style='display:none;' ><a href=javascript:updateFriend("+data[i].ID+",'"+data[i].GroupName+"')  title='属性'><img border='0' src='/Themes/Standard/Images/sz1.gif'/></a> &nbsp;&nbsp;<a href=javascript:delFriend("+data[i].ID+",'"+data[i].GroupName+"') title='删除'><img border='0' src='/Themes/Standard/Images/del1.gif'/></a></span></li>");    
                 if((i%column)==0&&i>0)
                 {                   
                      html+="<tr>";    
                 }        
                 html+="<td style='padding:5px;'><input type='radio' value='"+data[i].ID+"' id='selectGroupBox' name='selectGroupBox'></input>"+data[i].GroupName+"</td>";
                 if(i==data.length-1){
                     var k =data.length%column;                    
                     for(var p=0;p<k;p++){
                         html+="<td style='padding:5px;'></td>";
                     }
                 }
                 if(i%column==(column-1)&&i>0)
                 {  
                    html+="</tr>";
                 }   
             });
             gs.html(html);
             lgi.hide();
         },
         error:function(){
             lgi.hide();
             alert('error');                 
         }             
     });        
     lgi.show();       
 }
 
 function updateFriendGroup(id,friendName){
         currentFriendID=id;
         efc.show();
         efc.dialog({width:380,height:250,overlay: { opacity: 0.5, background: "black" },modal:true});
         tip.html(friendName+'是您的：');
         var rb =  $("#groupSelector  input[@value='"+currentGroupID+"']");        
         rb.attr('checked',true);
 }
 
 function updateFriend(id,groupName){
     gi.val(id);
     ug.val(groupName);
     egc.show();
     egc.dialog({width:380,height:100,overlay: { opacity: 0.5, background: "black" },modal:true});
 };
 
 function delFriend(id,groupName){
     if(!confirm('真的要删除好友分组:['+groupName+']?'))
     {
         return;
     }
     $.ajax({
         data:"groupID="+id,       
         type:'POST',
         url:"DeleteGroup",             
         success:function(res){                 
             getGroups();  
             alert('删除好友分组成功！');                                
         },
         error:function(){
             alert('delete group error!');                  
         }
     });         
 }
 
 function find(){
    if(kw.val()=='')
    {
        alert('请输入查询关键字！');
        return;
    }
    getAllFriend();
 }
 
 function getAllFriend(){
       var postData = 'kw='+escape(kw.val());
       $.ajax({
         type:'GET',
         url:'GetFriendsByUserID',
         data:postData,
         dataType:'json',
         success:function(res){
             var data = eval(res);     
             $('#friendList tr').remove();
             $.each(data,function(i){                     
                 fl.append("<tr><td style='padding:5px;'><img src='"+data[i].LogoUrl+"' width='50px' height='50px'/></td><td  valign='top'  style='padding-top:10px;'><a href='javascript:void(0);'>"+data[i].TrueName+"</a><div style='margin-top:20px;margin-left:4px;'><a href=javascript:updateFriendGroup("+data[i].ID+",'"+data[i].TrueName+"');>属性</a>&nbsp;&nbsp;<a href=javascript:showDel("+data[i].ID+",'"+data[i].TrueName+"');>删除</a></div></td></tr>");
             });             
             fl.show();
             lfi.hide();                 
         },
         error:function(){
             fl.hide();
             lfi.hide();
             alert('error');                 
         }             
     });
     lfi.show();   
 }
 function getFriend(id,groupName){      
     if(id<=0){
          getAllFriend();
          return;
     }
     currentGroupID=id;
     currrentGroupName = groupName;  
     msg.html('分组:['+groupName+']中有如下好友：');
     $.ajax({
         type:'GET',
         url:'GetFriends',
         data:'groupID='+id,
         dataType:'json',
         success:function(res){
             var data = eval(res);     
             $('#friendList tr').remove();
             $.each(data,function(i){                     
                 fl.append("<tr><td style='padding:5px;'><img src='"+data[i].LogoUrl+"' width='50px' height='50px'/></td><td  valign='top'  style='padding-top:10px;'><a href='javascript:void(0);'>"+data[i].TrueName+"</a><div style='margin-top:20px;margin-left:4px;'><a href=javascript:updateFriendGroup("+data[i].ID+",'"+data[i].TrueName+"');>属性</a>&nbsp;&nbsp;<a href=javascript:showDel("+data[i].ID+",'"+data[i].TrueName+"');>删除</a></div></td></tr>");
             });             
             fl.show();
             lfi.hide();                 
         },
         error:function(){
             fl.hide();
             lfi.hide();
             alert('error');                 
         }             
     });
     lfi.show();
 }
 
 function showDel(friendUserID,friendName){
     delf.show();
     fi.val(friendUserID);
     delf.dialog({width:420,height:240,overlay: { opacity: 0.5, background: "black" },modal:true});
     var dc = $('#delContent');
     dc.html('　　断开你和'+friendName+'之间的好友关系，你也将不再出现在'+friendName+'的好友列表中，但系统不会发消息通知对方。<br />　　如果只是不想在你的好友动态中显示有关'+friendName+'的动态，建议你使用“好友动态屏蔽”功能。 ');       
 }
